var basePath = "http://127.0.0.1:80/GraduationProject";
//var basePath = "http://123.206.33.201/gp/";

var num;

var userId ;

$(function(){
	$("#header").load("header.html",function(){
		ifLogin();
	});
	$("#footer").load("footer.html");
	$("#fixedNav").hide();
	
	showFixedNav();
	
});

/**
 * 显示固定导航
 */
function showFixedNav(){
	var last_scroll_position;
	$("#fixedNav").hide();

	window.addEventListener('scroll', function(e) {
	  last_scroll_position = window.scrollY;

	  if (last_scroll_position > 200 ) {
		$("#fixedNav").fadeIn();

	  } else if (last_scroll_position < 200) {
		$("#fixedNav").hide();
	  }
	});
}

/**
 * 判断是否登录
 */
function ifLogin(){
	var userName;
	var userInfo =  getUserInfo();
	if(userInfo){
		userName = userInfo.loginName;
		userId = userInfo.id;
		getAllShopCartInfo();
	}else{
		$("#shopCardLink span.text").html("0");
	}
	if(userName == null || userName == ""){
		$("#noLogin").show();
		$("#noRegisted").show();
		$("#doLogin").hide();
		$("#loginOut").hide();
		
	}else if(userName != null || userName != ""){
		$("#noLogin").hide();
		$("#noRegisted").hide();
		$("#doLogin").show();
		$("#loginOut").show();
		$("#userNickName").html(userInfo.nickName);
		loginOut();
	}
	
	// 点击购物车触发
	$("#shopCardLink").on("click",function(){
		if(userName == null || userName == ""){
			alert("您还没有登录");
		}else if(userName != null || userName != ""){
			window.location.href = "shopCard.html";
		}
	});
	
	// 点击购物车触发
	$("#myOrderLink").on("click",function(){
		if(userName == null || userName == ""){
			alert("您还没有登录");
		}else if(userName != null || userName != ""){
			window.location.href = "orderManage.html";
		}
	});
}

/**
 * 退出系统
 */
function loginOut(){
	$("#loginOut").on("click",function(){
		$.ajax({
    		type:"post",
    		url:"http://127.0.0.1:80/GraduationProject/outLogin",
    		async:true,
    		success:function(data){
    			if(data){
    				if(data.status == "200"){
    					window.location.href='login.html';
    				}else{
    					alert("操作失敗");
    				}
    			}
    		}
    	});
	});
}

/**
 * 获取当前用户信息
 */
function getUserInfo(){
	var currentUser;
	$.ajax({
		type:"get",
		url:basePath + "/user/detail",
		async:false,
		success:function(data){
			if(data){
				currentUser = data.obj;
			}
		}
	});
	return currentUser;
}

/**
 * 获取当前登录用户下的所有购物车信息
 */
function getAllShopCartInfo(){
	$.ajax({
		type:"get",
		url:basePath + "/cart/list",
		async:true,
		success:function(data){
			if(data){
				num = data.obj.length;
				$("#shopCardLink span.text").html(num);
			}
		}
	});
}

/**
 * 跳轉到商品展示頁
 */
function hrefAllCommodityPage(obj){
	sessionStorage.setItem("dataStyle",$(obj).attr("data-style"));
	window.location.href = "allCommodity.html";
}

/**
 * 将form提交的数据转化成形如{name:value...}的json
 */
$.fn.serializeObject = function () {
    var o = {};

    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });

    return o;
};

/**
 * 格式化时间
 */
Date.prototype.Format = function (fmt) { //author: tony  
    var o = {  
        "M+": this.getMonth() + 1, //月份  
        "d+": this.getDate(), //日  
        "h+": this.getHours(), //小时  
        "m+": this.getMinutes(), //分  
        "s+": this.getSeconds(), //秒  
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度  
        "S": this.getMilliseconds() //毫秒  
    };  
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));  
    for (var k in o)  
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));  
    return fmt;  
}; 











